package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * @author feizns
 * @since 2020/1/9
 */
public class MajorityElement {

    public static void main(String[] args) {
        System.out.println(majorityElement(new int[]{10, 9, 9, 9, 10}));
    }

    public static int majorityElement(int[] num) {
        int count = 1;
        int val = num[0];
        for (int i = 1; i < num.length; i++) {
            if ( count == 0 )
                val = num[i];
            count = num[i] == val ? count + 1 : count - 1;
        }
        return val;
    }

}
